package cn.innosmart.aq.wifiset;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.util.SharedUtil;

/**
 * 显示Wifi列表适配器
 * Created by David on 2016/3/4.
 */
public class WifiListAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<WifiBean> wifis;
    private View.OnClickListener onClickListener;

    /**
     * 构造函数
     *
     * @param context
     */
    public WifiListAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
        wifis = new ArrayList<>();
    }

    /**
     * 更新数据并刷新UI
     *
     * @param wifiBeans
     */
    public void refreshData(ArrayList<WifiBean> wifiBeans) {
        wifis = wifiBeans;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return wifis.size();
    }

    @Override
    public Object getItem(int position) {
        return wifis.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.wifi_list_item, null);
            holder.ivWifi = (ImageView) convertView.findViewById(R.id.iv_wifi);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tvStatus = (TextView) convertView.findViewById(R.id.tv_status);
            holder.rlBg = (RelativeLayout) convertView.findViewById(R.id.rl_bg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        WifiBean wifiBean = wifis.get(position);
        StringBuilder stringBuilder = null;
        //根据WIfi的安全类型，列表上显示不同的文字    安全、开放
        if (TextUtils.isEmpty(wifiBean.enctype)) {
            holder.ivWifi.setImageResource(R.drawable.wifi_list_item_lock_icon);
            stringBuilder = new StringBuilder(context.getString(R.string.wifi_safe));
        } else {
            if (wifiBean.enctype.equals("NONE")) {
                holder.ivWifi.setImageResource(R.drawable.wifi_list_item_icon);
                stringBuilder = new StringBuilder(context.getString(R.string.wifi_open));
            } else {
                holder.ivWifi.setImageResource(R.drawable.wifi_list_item_lock_icon);
                stringBuilder = new StringBuilder(context.getString(R.string.wifi_safe));
            }
        }
        //根据WIfi的状态 显示 wifi是否已连接
        if (wifiBean.status == 1) {
            holder.tvStatus.setText(R.string.wifi_connect);
        } else if (SharedUtil.getInstance().readWifiInfo(wifiBean.ssid) != null) {
            //本地查找是否找到该Wifi，若已找到则显示  保存
            if (!TextUtils.isEmpty(SharedUtil.getInstance().readWifiInfo(wifiBean.ssid).toString())) {
                stringBuilder.insert(0, context.getString(R.string.wifi_save));
                holder.tvStatus.setText(stringBuilder.toString());
            } else {
                holder.tvStatus.setText(stringBuilder.toString());
            }
        } else {
            holder.tvStatus.setText(stringBuilder.toString());
        }
        holder.tvTitle.setText(wifiBean.ssid);
        if (onClickListener != null) {
            holder.rlBg.setTag(wifiBean);
            holder.rlBg.setOnClickListener(onClickListener);
        }
        return convertView;
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    static class ViewHolder {
        RelativeLayout rlBg;
        TextView tvTitle;
        TextView tvStatus;
        ImageView ivWifi;
    }
}
